package com.zhenwei.zscm.repo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.entity.SequencesEntity;
import com.zhenwei.zscm.mapper.ISequencesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SequencesRepo extends ServiceImpl<ISequencesMapper, SequencesEntity> {

    /**
     * 获取售后工单序列
     * @return 自增序列值
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
    public synchronized long getOrderSequence() {
        // 先更新序列值
        baseMapper.updateCurrentValue("ORDER");
        // 查询新的序列值
        SequencesEntity sequence = baseMapper.selectOne(new LambdaQueryWrapper<SequencesEntity>()
                .eq(SequencesEntity::getUseFor, "ORDER"));
        return sequence.getCurrentValue();
    }

    /**
     * 获取发货工单号自增序列
     * @return 自增序列值
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
    public synchronized long getShippingOrderSequence() {
        // 先更新序列值
        baseMapper.updateCurrentValue("SHIPPING_ORDER");
        // 查询新的序列值
        SequencesEntity sequence = baseMapper.selectOne(new LambdaQueryWrapper<SequencesEntity>()
                .eq(SequencesEntity::getUseFor, "SHIPPING_ORDER"));
        return sequence.getCurrentValue();
    }
}
